$('#workChoose').append(showBtn());

var dataUrl = webConfig.webUrl + '/sseSubStation/getSubStationPageList',
    grid_selector = "#grid-table",
    pager_selector = "#grid-pager";

$(function() {

    $(grid_selector).jqGrid({

        height: 'auto',

        //是否允许显示/隐藏，只有caption不为空时有效
        hidegrid: false,

        //是否显示行号
        rownumbers: true,

        //table → caption
        caption: "站点管理列表",

        //显示的标题
        colNames: ['站点编号', '站点名称', '状态', '操作'],

        colModel: [{
            name: 'subStationNo',
            index: 'childId',
            sortable: false,
            align: 'center'
        }, {
            name: 'subStationName',
            index: 'childName',
            sortable: false,
            align: 'center'
        }, {
            name: 'status',
            index: 'status',
            sortable: false,
            align: 'center',
            formatter: function(x) {
                if (x == "1") {
                    return "已启用";
                } else {
                    return "已禁用";
                }
            }
        }, {
            name: 'id',
            index: 'id',
            align: 'center',
            sortable: false,
            formatter: function(x, y, z) {
                var strbtn = showBtn('right');
                if (strbtn.indexOf('update') > -1) {
                    strbtn = strbtn.replace('update(', 'update(\'' + x + '\'');
                }
                if (z.status == "1") {
                    if (strbtn.indexOf('disable') > -1) {
                        strbtn = strbtn.replace('disable();"', 'disable(\'' + x + '\');"');
                    }
                    if (strbtn.indexOf('enable') > -1) {
                        strbtn = strbtn.replace('enable();"', 'enable(\'' + x + '\');" disabled');
                    }
                    if (strbtn.indexOf('del') > -1) {
                        strbtn = strbtn.replace('del();"', 'del(\'' + x + '\');" disabled');
                    }
                } else {
                    if (strbtn.indexOf('disable') > -1) {
                        strbtn = strbtn.replace('disable();"', 'disable(\'' + x + '\');" disabled');
                    }
                    if (strbtn.indexOf('enable') > -1) {
                        strbtn = strbtn.replace('enable();"', 'enable(\'' + x + '\');"');
                    }
                    if (strbtn.indexOf('del') > -1) {
                        strbtn = strbtn.replace('del(', 'del(\'' + x + '\'');
                    }
                }
                return strbtn;
            }
        }],

        //可选显示的行数
        rowList: webConfig.pager.ROWLIST,

        //默认显示行数
        rowNum: webConfig.pager.PAGESIZE,

        //总条数
        viewrecords: true,

        //分页层 id
        pager: pager_selector,

        //分页参数
        prmNames: {
            page: 'pageIndex',
            rows: "pageSize",
            order: null,
            search: null,
            sort: null,
            nd: null,
            sidx: null
        },

        //提交方式，默认get
        mtype: 'POST',

        postData: setPostData(),

        //数据格式
        datatype: "json",

        jsonReader: {
            repeatitems: true, //是否允许乱序显示数据,true:不可以
            root: "result.list", //json中代表实际模型数据的入口
            total: "result.pages", //总页数
            page: "result.pageNum", //当前页
            records: "result.total" //总条数
        },

        //数据来源地址
        url: dataUrl,

        //加载完成后
        loadComplete: function(xhr) {
            checkResultCode({
                code: xhr.code,
                msg: xhr.message
            }, xhr.message, 0);
            var table = this;
            setTimeout(function() {
                updatePagerIcons(table);
            }, 0);
        }
    }).jqGrid('setGridWidth', $(".tab-pane").width());

    //设置grid宽度为自适应,适应比例根据每列设置的宽度
    $(window).on('resize.jqGrid', function() {
        setIframeHeight();
        $(grid_selector).jqGrid('setGridWidth', $(".tab-pane").width());
    });

});

//设置参数
function setPostData() {
    return {
        subStationNo: $.trim(document.getElementById('sonSiteId').value),
        subStationName: $.trim(document.getElementById('sonSiteName').value),
        status: document.getElementById('status').value,
        pageSize: webConfig.pager.PAGESIZE
    };
}

//查询
function query() {
    var sonSiteId = document.getElementById('sonSiteId').value,
        sonSiteName = document.getElementById('sonSiteName').value;
    if (sonSiteId && !validator($.trim(sonSiteId), 'nonSpecial2')) {
        parent.layer.alert('请输入正确的站点编号,不能包含特殊字符！', {
            icon: 2
        });
        return;
    }
    if (sonSiteName && !validator($.trim(sonSiteName), 'nonSpecial2')) {
        parent.layer.alert('请输入正确的站点名称,不能包含特殊字符！', {
            icon: 2
        });
        return;
    }

    var reloadParams = {
        url: dataUrl,
        postData: setPostData()
    };

    $(grid_selector).jqGrid("setGridParam", reloadParams).trigger("reloadGrid", [{
        page: 1
    }]);
}

/**
 * 添加站点
 */
function add() {
    updateSub();
}

/**
 * 修改站点
 * @param  {[type]} id [description]
 * @return {[type]}    [description]
 */
function update(id) {
    updateSub(id);
}

/**
 * 添加/修改站点
 * @param  {[type]} id [description]
 * @return {[type]}    [description]
 */
function updateSub(id) {
    parent.layer.open({
        scrollbar: false,
        type: 2,
        title: (id ? '编辑' : '添加') + '站点',
        shadeClose: true,
        shade: [0.7],
        area: ['768px', '450px'],
        content: 'html/system/childSiteUpdate.html' + (id ? '?id=' + id : ''),
        end: function() {
            query();
        }
    });
}

bodyKeyDown(query);

/**
 * 禁用
 * @param  {[type]} id     [description]
 * @param  {[type]} status [description]
 * @return {[type]}        [description]
 */
function disable(id) {
    changeStatus(id, 2);
}

/**
 * 启用
 * @param  {[type]} id     [description]
 * @param  {[type]} status [description]
 * @return {[type]}        [description]
 */
function enable(id) {
    changeStatus(id, 1);
}

/**
 * 修改站点状态
 * @param  {[type]} id     站点Id
 * @param  {[type]} status 状态
 * @return {[type]}        [description]
 */
function changeStatus(id, status) {
    if (id) {
        parent.layer.confirm('确定要修改状态？', {
            icon: 3,
            title: '提示'
        }, function(index) {
            $.ajax({
                url: webConfig.webUrl + '/sseSubStation/changeSubStationStatus',
                type: 'post',
                dataType: 'json',
                data: {
                    id: id,
                    status: status
                },
                success: function(data) {
                    checkResultCode({
                        code: data.code,
                        msg: data.message
                    }, '状态修改成功', 1, 0, function() {
                        query();
                    });
                }
            });
        });
    }
}

/**
 * 删除站点
 * @param  {[type]} id [description]
 * @return {[type]}    [description]
 */
function del(id) {
    if (id) {
        parent.layer.confirm('确定要删除此数据？', {
            icon: 3,
            title: '提示'
        }, function(index) {
            $.ajax({
                url: webConfig.webUrl + '/sseSubStation/deleteSubStation',
                type: 'post',
                dataType: 'json',
                data: {
                    id: id
                },
                success: function(data) {
                    checkResultCode({
                        code: data.code,
                        msg: data.message
                    }, '删除成功', 1, 0, function() {
                        query();
                    });
                }
            });
        });
    }
}